package pers.cz.postgirl.core.async;

import com.lmax.disruptor.RingBuffer;
import pers.cz.postgirl.core.Spy;

/**
 * @program: postgirl-client
 * @description: 事件生产者，将事件推送到队列
 * @author: Cheng Zhi
 * @create: 2023-05-30 16:43
 **/
public class SpyEventProducer {

    private RingBuffer<SpyEvent> ringBuffer;

    public SpyEventProducer(RingBuffer<SpyEvent> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    public void pushData(Spy spy) {
        ringBuffer.getCursor();
        try {
            long sequence =  ringBuffer.tryNext();
            SpyEvent spyEvent = ringBuffer.get(sequence);
            spyEvent.set(spy);
            ringBuffer.publish(sequence);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
